﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace ggprot
{
    public class GGLogin : GGHeader
    {
        private int uin;
        private string language;
        private byte hashType;
        private byte[] hash;
        private int status;
        private int flags;
        private int features;
        private int localIp;
        private short localPort;
        private int externalIp;
        private short externalPort;
        private byte imageSize;
        private byte unknown2;
        private int versionLen;
        private string version;
        private int descriptionSize;
        private string description;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public int DescriptionSize
        {
            get { return descriptionSize; }
            set { descriptionSize = value; }
        }

        public string Version
        {
            get { return version; }
            set { version = value; }
        }

        public int VersionLen
        {
            get { return versionLen; }
            set { versionLen = value; }
        }

        public byte Unknown2
        {
            get { return unknown2; }
            set { unknown2 = value; }
        }

        public byte ImageSize
        {
            get { return imageSize; }
            set { imageSize = value; }
        }

        public short ExternalPort
        {
            get { return externalPort; }
            set { externalPort = value; }
        }

        public int ExternalIp
        {
            get { return externalIp; }
            set { externalIp = value; }
        }

        public short LocalPort
        {
            get { return localPort; }
            set { localPort = value; }
        }

        public int LocalIp
        {
            get { return localIp; }
            set { localIp = value; }
        }

        public int Features
        {
            get { return features; }
            set { features = value; }
        }

        public int Flags
        {
            get { return flags; }
            set { flags = value; }
        }

        public int Status
        {
            get { return status; }
            set { status = value; }
        }

        public byte[] Hash
        {
            get { return hash; }
            set { hash = value; }
        }

        public byte HashType
        {
            get { return hashType; }
            set { hashType = value; }
        }

        public string Language
        {
            get { return language; }
            set { language = value; }
        }

        public int Uin
        {
            get { return uin; }
            set { uin = value; }
        }

        public override void Write(NetworkStream stream)
        {
            base.Write(stream);

            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write(uin);
            writer.Write(Encoding.ASCII.GetBytes(language.ToCharArray(), 0, 2));
            writer.Write(hashType);
            writer.Write(hash);
            writer.Write(status);
            writer.Write(flags);
            writer.Write(features);
            writer.Write(localIp);
            writer.Write(localPort);
            writer.Write(externalIp);
            writer.Write(externalPort);
            writer.Write(imageSize);
            writer.Write(unknown2);
            writer.Write(versionLen);
            writer.Write(Encoding.ASCII.GetBytes(version.ToCharArray(), 0, versionLen));
            writer.Write(descriptionSize);
            if (descriptionSize > 0)
                writer.Write(Encoding.ASCII.GetBytes(description.ToCharArray(), 0, descriptionSize));

            writer.Flush();
        }
    }
}
